package com.example.j202411.views.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;


import com.example.j202411.adapter.CatalogAdapter;
import com.example.j202411.contract.CatalogContact;
import com.example.j202411.databinding.ActivityCatalogBinding;
import com.example.j202411.Model.CatalogEntity;
import com.example.j202411.viewmodel.CatalogModel;

import java.util.ArrayList;
import java.util.List;

public class CatalogACtivity extends AppCompatActivity implements CatalogContact.CatalogView, View.OnClickListener {
    private CatalogContact.CatalogPresenter presenter;
    private CatalogAdapter adapter;
    private List<CatalogEntity.ResultDTO> mlist;
    private ActivityCatalogBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCatalogBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    private void init() {
        presenter = new CatalogModel(this, this);
        mlist = new ArrayList<>();
        adapter = new CatalogAdapter(mlist, this);
        binding.recycleCatalog.setLayoutManager(new GridLayoutManager(this, 1)); // 或者使用 LinearLayoutManager
        binding.recycleCatalog.setAdapter(adapter);
        presenter.getData("0");
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void showData(CatalogEntity catalogEntity) {
        if (catalogEntity != null && catalogEntity.getResult() != null) {
            List<CatalogEntity.ResultDTO> newData = catalogEntity.getResult();
            Log.d("6666666666", "Received data size: " + newData.size());
            mlist.clear();
            mlist.addAll(newData);
            adapter.updateData(newData);
        } else {
            Log.e("66666666666", "CatalogEntity or its result is null");
        }
    }
}
